<?php

namespace App\Http\Resources\Admin;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SysDepartmentOptionsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $data = [
            'value' => $this->id,
            'label' => $this->name,
            'children' => SysDepartmentOptionsResource::collection($this->whenLoaded('children')),
        ];

        return $data;
    }

    /**
     * Transform the children array.
     *
     * @param  array  $children
     * @return array
     */
    protected function transformChildren($children)
    {
        return collect($children)->map(function ($child) {
            return [
                'value' => $child['id'],    // 映射为 value
                'label' => $child['name'],  // 映射为 label
            ];
        })->toArray();
    }
}
